package com.crm.customers.controller;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.crm.base.controller.BaseController;
import com.crm.customers.model.Express;
import com.crm.customers.service.ExpressService;
import com.crm.user.model.UserInfo;
import com.crm.user.service.UserService;
import com.crm.util.Pager;
@Controller
public class ExpressController extends BaseController {
	
	@Autowired
	private ExpressService expressService;
	@Autowired
	private UserService userService;
	
	/**
	 * 快递url拼接
	 * */
	@RequestMapping(value="/customer/expresslisturl")
	public String intoExpressListUrl(String keyWord, String createuser,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		String url = "/customer/expresslist";
		url += "p"+getSkipToPage();
	 
		if (createuser != null && createuser.length() > 0) {
			url += "_c"+ createuser;
		}
		if (!Strings.isNullOrEmpty(keyWord)){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(keyWord, "UTF-8"),"UTF-8");
		}
		return "redirect:"+url; 
	}
	
	/**
	 * 快递url拼接
	 * */
	@RequestMapping(value="/customer/myexpresslisturl")
	public String myExpressListUrl(String keyWord, String createuser,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		String url = "/customer/myexpresslist";
		url += "p"+getSkipToPage();
	 
		if (createuser != null && createuser.length() > 0) {
			url += "_c"+ createuser;
		}
		if (!Strings.isNullOrEmpty(keyWord)){
			url += "?kwd=" + URLEncoder.encode(URLEncoder.encode(keyWord, "UTF-8"),"UTF-8");
		}
		return "redirect:"+url; 
	}
	
	
	/**
	 * 快递信息列表
	 * */
	@RequestMapping(value="/customer/myexpresslist{k}")
	public String myExpressListPage(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		Integer pageNo = getSkipToPage();
		Integer pageSize = 10;
		String keyWord = null;
		Long createuser = null;
		
		String key[] = k.split("_");
		for (String kk : key) {
			if (kk.indexOf("p") > -1) {
				pageNo = Integer.parseInt(kk.replace("p",""));
			}  else if (kk.indexOf("c") > -1) {
				createuser = Long.parseLong(kk.replace("c", ""));
			}
		}
		if(request.getParameter("kwd")!=null){
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
		}
		
		//获取代办建造师列表
		Express cc = new Express();
		cc.setCreateuser(getLoginUser().getId());
		cc.setKeyWord(keyWord);
		
		List<Express> list = expressService.getExpress(cc, pageNo, pageSize);
		
		//获取所有人
		List<UserInfo> userlist = userService.getAllUserListByDeptId(null);
		
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/customer/myexpresslist");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("createuser", createuser);
		params.put("keyWord", keyWord);
		pager.setParams(params);
		request.setAttribute("pager", pager);
		request.setAttribute("userlist", userlist);
		request.setAttribute("uid", getLoginUser().getId());
		
		return "/customermanage/myexpresslist";
	}
	/**
	 * 快递信息列表
	 * */
	@RequestMapping(value="/customer/expresslist{k}")
	public String intoExpressListPage(@PathVariable String k,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		Integer pageNo = getSkipToPage();
		Integer pageSize = 10;
		String keyWord = null;
		Long createuser = null;
		
		String key[] = k.split("_");
		for (String kk : key) {
			if (kk.indexOf("p") > -1) {
				pageNo = Integer.parseInt(kk.replace("p",""));
			}  else if (kk.indexOf("c") > -1) {
				createuser = Long.parseLong(kk.replace("c", ""));
			}
		}
		if(request.getParameter("kwd")!=null){
			keyWord = (URLDecoder.decode(URLDecoder.decode(request.getParameter("kwd"), "UTF-8"),"UTF-8"));
		}
		
		//获取代办建造师列表
		Express cc = new Express();
		if(createuser!=null&&createuser>0){
			cc.setCreateuser(createuser);
		}
		cc.setKeyWord(keyWord);
		
		List<Express> list = expressService.getExpress(cc, pageNo, pageSize);
		
		//获取所有人
		List<UserInfo> userlist = userService.getAllUserListByDeptId(null);
		
		Pager pager = new Pager(pageNo, pageSize);
		pager.setResultList(list);
		pager.setUrl("/customer/expresslist");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("createuser", createuser);
		params.put("keyWord", keyWord);
		pager.setParams(params);
		request.setAttribute("pager", pager);
		request.setAttribute("userlist", userlist);
		request.setAttribute("uid", getLoginUser().getId());
		
		return "/customermanage/expresslist";
	}
	
	/**
	 * 保存快递信息
	 * */
	@RequestMapping(value="/customer/saveexpress")
	@ResponseBody
	public String saveExpress(@ModelAttribute Express c,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes) throws Exception{
		
		//所以第一步是把获取的代办存到数据库
		//将审批状态设置为0
		c.setUpdateuser(getLoginUser().getId());
		//保存代办事项
		c = expressService.saveExpress(c);
		 
		return JSON.toJSONString(1);
	}
	
	/**
	 * 删除快递信息
	 * */
	@RequestMapping(value="/customer/deleteexpress")
	@ResponseBody
	public String deleteExpress(Long id,HttpServletResponse response,HttpServletRequest request,
			RedirectAttributes redirectAttributes){
		int result = 0;
		Express c = new Express();
		c.setId(id);
		c.setIsdel(1);
		c.setUpdateuser(getLoginUser().getId());
		result = expressService.updateExpress(c);
		return JSON.toJSONString(result);
	}
}
